<?php

/**
 * Model UserSignUp
 *
 * @author nguyenmanhluu
 * @since Aug 12, 2013 - 9:24:38 PM
 */
class UserSignUp extends Users
{

    public $subscribe;

    function rules()
    {
        $rules   = parent::rules();
        $rules[] = array(
            'new_pass, firstname, lastname, repassword, mobile_phone',
            'required',
            'on' => 'signup,signup-frontend,signup-frontend-social'
        );
        $rules[] = array(
            'mobile_phone', 'check_validate_phone'
        );
        $rules[] = array(
            'repassword',
            'compare',
            'compareAttribute' => 'new_pass',
            'on'               => 'signup,signup-frontend, signup-frontend-social'
        );
        $rules[] = array(
            'email', 'check_email', 'on' => 'signup-frontend, signup-frontend-social'
        );
        $rules[] = array(
            'firstname, lastname, email, mobile_phone',
            'required',
            'on' => 'booking'
        );
        return $rules;
    }

    public function check_validate_phone($attribute, $params)
    {
        if ($this->$attribute != '')
        {
            $a = trim($this->$attribute);
            $a = str_replace(' ', '', $a);
            if (!in_array($a[0], array('6', '9', '8')))
            {
                $this->addError($attribute, Yii::t('main', 'Please provide a valid phone number'));
            }
            return;
        }
    }

    public function check_email($attribute, $params)
    {

        if (!$this->check_duplicate())
        {
            $this->addError($attribute, Yii::t('main', 'Email has already been registered'));
            return;
        }
    }

    public function check_duplicate()
    {
        if ($this->isNewRecord)
        {

            $crite            = new CDbCriteria();
            $crite->condition = 'email=:email AND type=:type';
            $crite->params    = array(':email' => $this->email, ':type' => Users::TYPE_CUSTOMER);

            $user = Users::model()->find($crite);

            if ($user)
            {
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
    }

}

/** 
 * End file UserSignUp
 */
